#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "dominion.h"
#include "rngs.h"
#include <math.h>

#define doStop 0 // set to 1 if you want a failed assert to stop the program

int testFailed;

void resetFlag(){
    testFailed = 0;
}

int testState(){
    return testFailed;
}

void incState(){
    testFailed++;
}

// converts card numbers to their string equivalents
char *cardToString(int number){
    switch(number){
        case 0:
            return "curse";
            break;
        case 1:
            return "estate";
            break;
        case 2:
            return "duchy";
            break;
        case 3:
            return "province";
            break;
        case 4:
            return "copper";
            break;
        case 5:
            return "silver";
            break;
        case 6:
            return "gold";
            break;
        case 7:
            return "adventurer";
            break;
        case 8:
            return "council_room";
            break;
        case 9:
            return "feast";
            break;
        case 10:
            return "gardens";
            break;
        case 11:
            return "mine";
            break;
        case 12:
            return "remodel";
            break;
        case 13:
            return "smithy";
            break;
        case 14:
            return "village";
            break;
        case 15:
            return "baron";
            break;
        case 16:
            return "great_hall";
            break;
        case 17:
            return "minion";
            break;
        case 18:
            return "steward";
            break;
        case 19:
            return "tribute";
            break;
        case 20:
            return "ambassador";
            break;
        case 21:
            return "cutpurse";
            break;
        case 22:
            return "embargo";
            break;
        case 23:
            return "outpost";
            break;
        case 24:
            return "salvager";
            break;
        case 25:
            return "sea_hag";
            break;
        case 26:
            return "treasure_map";
            break;
        default:
            return "";
            break;
    }
    return "";
}

void setState(struct gameState *state){
  int i;
  state->numPlayers = 2;
  for (i = adventurer; i <= treasure_map; i++){
    state->supplyCount[i] = 10;
    state->embargoTokens[i] = 0;
  }
  state->outpostPlayed = 0;
  state->outpostTurn = 0;
  state->whoseTurn = 0;
  state->phase = 0;
  state->numActions = 1; /* Starts at 1 each turn */
  state->coins = 0; /* Use as you see fit! */
  state->numBuys = 1; /* Starts at 1 each turn */
  for (i = 0; i< state->numPlayers; i++){
    state->handCount[i] = 0;
    state->deckCount[i] = 0;
    state->discardCount[i] = 0;
  }
  state->playedCardCount = 0;
}

void resetDeck(struct gameState *state){
    int i;
    int j;

  state->deckCount[0] = 0;
  state->deckCount[1] = 0;

  for (i = 0; i < state->numPlayers; i++)
    {
      state->deckCount[i] = 0;
      for (j = 0; j < 3; j++)
	{
	  state->deck[i][j] = estate;
	  state->deckCount[i]++;
	}
      for (j = 3; j < 10; j++)
	{
	  state->deck[i][j] = copper;
	  state->deckCount[i]++;
	}

	for (j = 0; j < 500; j++){
        state->discard[i][j] = -1;
        state->hand[i][j] = -1;
        state->playedCards[j] = -1;
        if (j >= 10)
            state->deck[i][j] = -1;
	}
    }
}

void assertMessage(char *message, int firstVal, int secondVal){
    printf(message, firstVal, secondVal);
    if (doStop == 1){
        assert(1 == 2);
    }
    incState();
}

// picks a random card to generate
int randomCard(struct gameState *state){
    int random;
    int number;
    number = floor((Random() * 20)) + 1;

    switch(number){
        case 1:
            random = adventurer;
            break;
        case 2:
            random = council_room;
            break;
        case 3:
            random = feast;
            break;
        case 4:
            random = gardens;
            break;
        case 5:
            random = mine;
            break;
        case 6:
            random = remodel;
            break;
        case 7:
            random = smithy;
            break;
        case 8:
            random = village;
            break;
        case 9:
            random = baron;
            break;
        case 10:
            random = great_hall;
            break;
        case 11:
            random = minion;
            break;
        case 12:
            random = steward;
            break;
        case 13:
            random = tribute;
            break;
        case 14:
            random = ambassador;
            break;
        case 15:
            random = cutpurse;
            break;
        case 16:
            random = embargo;
            break;
        case 17:
            random = outpost;
            break;
        case 18:
            random = salvager;
            break;
        case 19:
            random = sea_hag;
            break;
        case 20:
            random = treasure_map;
            break;
        case 21:
            random = randomCard(state);
            break;
    }

    return random;
}
